package com.ktjy.web.dao.impl;

import com.ktjy.web.dao.AttrValuesDao;
import com.ktjy.web.dao.BaseDao;
import com.ktjy.web.dao.UserDao;
import com.ktjy.web.entity.AttrValues;
import com.ktjy.web.entity.User;
import com.ktjy.web.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @version v1.0
 * @Author 郴州科泰
 * @Create 2024-07-24 - 21:30
 */
public class AttrValuesDaoImpl extends BaseDao implements AttrValuesDao {

    /**
     * 通过商品id查询t_goods_attr_values，得到商品属性值列表。
     * @param goodsId 商品id，来源的t_goods_info商品表的主键
     * @return 返回查询到的商品属性值列表
     */
    @Override
    public List<AttrValues> getAttrVals(int goodsId) {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        List<AttrValues> list=null;
        try{
            list=sqlSession.getMapper(AttrValuesDao.class).getAttrVals(goodsId);

        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return list;
    }

    /**
     * 通过库存sku查询t_attr_values表，得到sku的属性值列表。
     * @param stockSku 库存sku
     * @return 返回查询到的sku属性值列表
     */
    @Override
    public List<AttrValues> getSkuAttrVals(String stockSku) {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        List<AttrValues> list=null;
        try{
            list=sqlSession.getMapper(AttrValuesDao.class).getSkuAttrVals(stockSku);

        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return list;
    }

    @Override
    public List<AttrValues> selectAll() {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        List<AttrValues> list=null;
        try{
            list=sqlSession.getMapper(AttrValuesDao.class).selectAll();

        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return list;
    }

    @Override
    public AttrValues selectById(int id) {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        AttrValues attrValues=null;
        try{
            attrValues=sqlSession.getMapper(AttrValuesDao.class).selectById(id);

        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return attrValues;
    }

    @Override
    public int insert(AttrValues attrValues) {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        int count=0;
        try{
            count=sqlSession.getMapper(AttrValuesDao.class).insert(attrValues);

        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return count;
    }

    @Override
    public int update(AttrValues attrValues) {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        int count=0;
        try{
            count=sqlSession.getMapper(AttrValuesDao.class).update(attrValues);

        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return count;
    }

    @Override
    public int deleteById(int id) {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        int count=0;
        try{
            count=sqlSession.getMapper(AttrValuesDao.class).deleteById(id);

        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return count;
    }

    @Override
    public List<AttrValues> page(String name,int attrId,int pageNo, int pageSize) {

        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        List<AttrValues> list=null;
        try{
            list=sqlSession.getMapper(AttrValuesDao.class).page(name,attrId,pageNo,pageSize);

        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return list;
    }

    @Override
    public int count() {
        SqlSession sqlSession= MyBatisUtil.createSqlSession();
        int count=0;
        try{
            count=sqlSession.getMapper(AttrValuesDao.class).count();

        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return count;
    }
}
